<?php header("Access-Control-Allow-Origin:*");

if (empty($_ENV)) {
    $WEB_ROOT = dirname(dirname(dirname(dirname(__DIR__))));
    require $WEB_ROOT . "/vendor/autoload.php";
    $dotenv = Dotenv\Dotenv::createImmutable($WEB_ROOT);
    $dotenv->load();
}

if (empty($_ENV)) {
    echo "config file does't exist";exit;
}

// load app configs
$appConfigs = require __DIR__ . '/../config/app.config.php';

// load framework system
require $appConfigs['FRAMEWORK_ROOT'] . '/' . $appConfigs['FRAMEWORK_VER'] . '/SzSystem.class.php';
// parse app root
$appRoot = __DIR__; // e.g "/home/php/app/demo/apps/main/v0.1.1/www"
$wwwRelativePath = substr($appRoot, strpos($appRoot, 'apps')); // apps/main/v0.1.1/www
$appRoot = substr($appRoot, 0, strlen($appRoot) - 4); // remove tailing '/www'

// systemApi System.GmCmd
if (isset($_REQUEST['systemApi']) && in_array($_REQUEST['systemApi'], $appConfigs['SYSTEM_API'])) {
    $queryString = json_encode(array(array($_REQUEST['systemApi'],array())));
    $rawInputs = $queryString;
    if ($appConfigs['API_COMPRESS']) {
        $queryString = SzUtility::compress($queryString, false);
    }
    if ($appConfigs['BASE64_ENCODE']) {
        $queryString = SzUtility::base64Encode($queryString);
    }
    $_REQUEST["*"] = $queryString;
    $_REQUEST['halt'] = uniqid();
    $_REQUEST['sign'] = SzUtility::makeSign(array('*' => $rawInputs, 'halt' => $_REQUEST['halt']), $appConfigs['API_SIGN_SECRET']);
}

$_REQUEST["*"] = '[["unionCompetition.delTool",[]]]';

// initialize framework system
SzSystem::init($appRoot, $wwwRelativePath, $appConfigs['MODULE_ROOT'], $appConfigs['MODULE_VERS']);
// process the requests
SzController::get()->process(); exit(0); // execute & exit
